﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Diagnostics;
using System.Windows.Media;
using ODS.WebService.Login_Logout;
using ODS.Utils;
using Microsoft.Phone.Tasks;
using ODS.Config;


namespace ODS.UserControls
{
    public partial class SlidingMenu : UserControl
    {
        Image currentImageSelected;

        public SlidingMenu()
        {
            InitializeComponent();

            this.DataContext = App.MainViewModel.TotalMessageViewModel;
        }

        public int GetHeightTopBar()
        {
            return (int)this.topBar.Height;
        }

        public bool IsUnRegisterEventMouseForGrids = false;

        public void UnRegisterEventMouseForGrids()
        {
            IsUnRegisterEventMouseForGrids = true;

            //grid_profile.MouseEnter -= grid_MouseEnter;
            //grid_message.MouseEnter -= grid_MouseEnter;
            //grid_addNewDevice.MouseEnter -= grid_MouseEnter;
            //grid_support.MouseEnter -= grid_MouseEnter;
            //grid_systemStatus.MouseEnter -= grid_MouseEnter;
            //grid_logOut.MouseEnter -= grid_MouseEnter;

            //grid_profile.MouseLeave -= grid_MouseLeave;
            //grid_message.MouseLeave -= grid_MouseLeave;
            //grid_addNewDevice.MouseLeave -= grid_MouseLeave;
            //grid_support.MouseLeave -= grid_MouseLeave;
            //grid_systemStatus.MouseLeave -= grid_MouseLeave;
            //grid_logOut.MouseLeave -= grid_MouseLeave;

            grid_profile.Background = null;
            grid_message.Background = null;
            grid_addNewDevice.Background = null;
            grid_support.Background = null;
            grid_systemStatus.Background = null;
            grid_logOut.Background = null;

        }

        public void RegisterEventMouseForGrids()
        {
            IsUnRegisterEventMouseForGrids = false;

            grid_profile.Background = new SolidColorBrush(Colors.Transparent);
            grid_message.Background = new SolidColorBrush(Colors.Transparent);
            grid_addNewDevice.Background = new SolidColorBrush(Colors.Transparent);
            grid_support.Background = new SolidColorBrush(Colors.Transparent);
            grid_systemStatus.Background = new SolidColorBrush(Colors.Transparent);
            grid_logOut.Background = new SolidColorBrush(Colors.Transparent);
        }

        private void showImageSelected(Image image)
        {
            if (currentImageSelected != null)
                currentImageSelected.Visibility = Visibility.Collapsed;

            currentImageSelected = image;
            currentImageSelected.Visibility = Visibility.Visible;
        }

        public void HiddenImageSelected()
        {
            if (currentImageSelected != null)
                currentImageSelected.Visibility = Visibility.Collapsed;
        }

        //private void grid_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        //{
        //    //Grid currentItem = sender as Grid;
        //    //currentItem.Background = new SolidColorBrush(Colors.Gray);
        //}

        private void grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Grid currentItem = sender as Grid;
            currentItem.Background = new SolidColorBrush(Colors.Gray);
        }

        private void grid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Grid currentItem = sender as Grid;
            currentItem.Background = new SolidColorBrush(Colors.Transparent);

        }

        private void gridProfile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Grid currentItem = sender as Grid;
            currentItem.Background = new SolidColorBrush(Colors.Gray);

            showImageSelected(image_seletected_profile);

            App.mainPage.NavigationService.Navigate(new Uri("/../ProfilePage.xaml", UriKind.Relative));

        }

        private void gridMessage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Grid currentItem = sender as Grid;
            currentItem.Background = new SolidColorBrush(Colors.Gray);

            showImageSelected(image_seletected_message);

            App.mainPage.NavigationService.Navigate(new Uri("/../TotalMessagePage.xaml", UriKind.Relative));
        }

        private void gridAddNewDevice_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Grid currentItem = sender as Grid;

            showImageSelected(image_seletected_addNewDevice);

            App.mainPage.NavigationService.Navigate(new Uri("/../AddNewDevicePage.xaml", UriKind.Relative));

        }

        private void gridSupport_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            showImageSelected(image_seletected_support);

            EmailComposeTask emailComposeTask = new EmailComposeTask();
            emailComposeTask.To = "support@covexis.com";
            emailComposeTask.Subject = "Hello";
            emailComposeTask.Show();
        }

        private void gridSystemStatus_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            showImageSelected(image_seletected_systemStatus);

            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri("https://twitter.com/FireAntsODS");
            webBrowserTask.Show();

        }

        private void btn_aboutInfo_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.Uri = new Uri("http://ods.covexis.com");
            webBrowserTask.Show();
        }

        private  void gridLogOut_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Grid currentItem = sender as Grid;

            showImageSelected(image_seletected_logout);

            App.BackToLoginPage();

            logOut();

        }

        async void logOut()
        {
            await LogoutService.logout();

            await new WebBrowser().ClearCookiesAsync();

            //ClearCookies(new Uri(Constants.SERVER_URL));
            //App.cookieLoginContainer  = new CookieContainer();
        }

        public void ClearCookies(Uri uri)
        {
             var cookies = App.cookieLoginContainer.GetCookies(uri);
             foreach (Cookie cookie in cookies)
             {
                 cookie.Discard = true;
                 cookie.Expired = true;
             }
        }
        


        


    }
}
